'use strict';

const Controller = require('egg').Controller;
// 发送支付功能
const AlipaySDK = require('alipay-sdk').default
// 将支付信息 formdata 包裹住
const AlipayFormData = require('alipay-sdk/lib/form').default

class PayController extends Controller {
    async alipay() {
        const { ctx, config } = this;
        const alipaySdk = new AlipaySDK({
            appId: '2021000118644866', // 你自己的沙箱环境的appId
            privateKey: "MIIEowIBAAKCAQEAq7P4Ka9s44knW/Y2ex+PLV2oTwdn1Z2vbGlHVYcq4fhT2e/DPul8UVkRsxfhndGDRm8GQ0U9UAJavp/maCvGHIAU92Pln9I9KeSj+WDg/eT4SjD6T6Izs0jPjZGMXSjFgEizBT52lGW5m/yAQy7Ib4xfGB6BEr+lqVGgA3rYjrhM6U8Ff6N3KcIKBGrqDs4ntPSsVaxa703XygwOuWT1AkK1CVmuqfwi7tlBJmBsCzapI6m0BAKjZ56WUfM5vn/qs3SNX1zq7gveMsQX1kUt6InItXB32V4+AqFAskRqfhcvQcHS0LlO63lxFcEeueDZR0SBl7ExHgASlBpQdya8HQIDAQABAoIBAH8XNf+83k9JDL4P72GRCYxdv5/Hbn76OYd4IZnX89DQ21f8FTuy3RblY6l5asFC+/UdZrhSPsDDDWH9vzCp/fjIgTxgT+3NUwJ4PzruD3s+iN15hT1+ZOA2a5JxAkFOVWnP5dpi0qii5xDjhBi2Pxv5/cU/hvBWtIswZM1Ol46C+K7R+fHB8leMDcdgylC2BgTOPGxHc47gKquYijgvfRnaXuuCswU4mlolAYhI2Nh3pc3WXIz6CwVXWTEscpJCBC1P7vEEFsgXd7izReNipmzdsFl3n9Va72WW8K2nw5K65H326ozRQx+S0neNWbb+WZI6td3ymFXY9Gopi7lciS0CgYEA8gpT0Upm+WqtMBKUm29JZrK1HHJQI0NQTss3TCm8a/7qHDV8BfkgZKoyovt3cfwESS4IwEHyiZBqrPmpD0S9utKRh6gkFHyU76ZLuV63XLvL3y96gU/CXUi342q42AuCI2F8GXOUOvlpZ+QPPcAs9APAZ7T1VpT4HqrtKHfuorcCgYEAtZsganoAQpGSlT8x80ZCtt8x1EJPtKdysJ0pY48zZFKu/9JHhSywR/NqFTPVBF/9oEcMEJ+dRqIIBEyHQhPcJj7a54Yt42CJ6l+Yw4ELjSxRrW3VMbqIBpKgA0VQ2XvSB9Awp3LV+G4iSOvzN27JQAa0EQ72BPPT499O05NI88sCgYAe0dPUbY3pKn09BtCnbidBjXXkVmX/531dWyECcLoOYlo7F1BgjlVinA1wlWajO/zruJHisUMJ4zrCJdQyHoWuOUvQo0dc2HQ6g8lWpEni5ALUu33040wThsPrkFZTJDwA91PPBdmVHNcgJHkQWt0egS/JxNAMo1Fozkj+gy7+xwKBgQCjWmQwIol1V4goo22EzCBaai1iJjuX3j71yoheO4MYrJ8pEbavfeEa90Lmy3bCtq1mrEGX+lr4G+tmIohP0Zlm9YbmVvWT31Ru84pv4PXJCKJ3g6IPVx/f+afhCUqONihYStZY6Jy7GCXSvhpKNGbmJqF17qUxEzn3cPhwq70lPQKBgH2pOviIuuddu8BdjWzTNV/dwUoSVNDf9zl7XhdMi4lfdnXOkDyomx19VL+HSwJDlRm5Eup7VbLXp2ABsZxVNrYE5rONHe94TJ7PWY8x34YWMtejeidPGlhGqH5vd8h/1z9/IQWY9VyT2zdBKNgMgXJ2QhenuCnmYvSZjiKPJx9/", // 私钥
            signType: 'RSA2', // 签名类型
            alipayPublicKey: 'MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAq7P4Ka9s44knW/Y2ex+PLV2oTwdn1Z2vbGlHVYcq4fhT2e/DPul8UVkRsxfhndGDRm8GQ0U9UAJavp/maCvGHIAU92Pln9I9KeSj+WDg/eT4SjD6T6Izs0jPjZGMXSjFgEizBT52lGW5m/yAQy7Ib4xfGB6BEr+lqVGgA3rYjrhM6U8Ff6N3KcIKBGrqDs4ntPSsVaxa703XygwOuWT1AkK1CVmuqfwi7tlBJmBsCzapI6m0BAKjZ56WUfM5vn/qs3SNX1zq7gveMsQX1kUt6InItXB32V4+AqFAskRqfhcvQcHS0LlO63lxFcEeueDZR0SBl7ExHgASlBpQdya8HQIDAQAB', // 支付宝公钥（不是应用公钥）
            gateway: 'https://openapi.alipaydev.com/gateway.do', // 网关地址
            timeout: 5000, // 网关超时时间
            camelcase: true // 是否把网关返回的下划线 key 转换为驼峰写法
        })

        const formData = new AlipayFormData();

        formData.setMethod('get'); //请求方式
        formData.addField('appId', '2021000118644866'); //用户id
        formData.addField('charset', 'utf-8'); //编码格式
        formData.addField('signType', 'RSA2');//注册信息格式
        formData.addField('bizContent', { //设置商户信息
            outTradeNo: new Date() * 1 + '', // 【必选】商户订单号：64个字符内，包含数字，字母，下划线；需要保证在商户端不重复
            productCode: 'FAST_INSTANT_TRADE_PAY', // 【必选】销售产品码(二维码)，目前仅支持FAST_INSTANT_TRADE_PAY
            totalAmount: '200', // 【必选】订单总金额，精确到小数点后两位
            subject: 'aaaaaaaaaaaaaaaaaa', // 【必选】 订单标题
            body: 'bbbbbbbbbbbbbbbbb' // 【可选】订单描述
        })
        console.log('formData:===============', formData)
        /**
         * exec对应参数：
         * method（调用支付宝api）
         * params（api请求的参数（包含“公共请求参数”和“业务参数”））
         * options（validateSign，formData，log）
         *
         * 统一下单支付 exec
         */
        ctx.body = await alipaySdk.exec('alipay.trade.page.pay', {}, { formData })
    }
}

module.exports = PayController;